<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name',children:'children'}"
      default-expand-all
      node-key="id"
      :show-checkbox="true"
      :check-strictly="true"
    />
    <el-row type="flex" justify="end" height="60">
      <el-button type="primary" @click="hsubmit">确定</el-button>
      <el-button type="info" @click="$emit('close')">取消</el-button>
    </el-row>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissions'
import { transDataToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    rolseid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadgetPermissionList()
  },
  methods: {
    async  hsubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.rolseid, permIds: permIds })
        this.$emit('close')
        console.log('assignPerm', res)
        // 保存数据
      } catch (err) {
        console.log('assignPerm', err)
      }
    },
    async loadgetPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        // transDataToTreeData 转化为树形结构
        this.list = transDataToTreeData(res.data)
        // info.data.permIds=["604f7e17f900be1850edb154"]
        const info = await getRoleDetail(this.rolseid)
        console.log(info)
        // 树节点的选择 设置node-key
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
